package com.dlc.yunguang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.model.InventoryRecordBean;
import com.dlc.yunguang.net.protocol.CountingProto;
import com.dlc.yunguang.R;
import com.dlc.yunguang.adapter.InventoryRecordAdapter;
import com.dlc.yunguang.adapter.decoration.RecyclerSpace;

import butterknife.BindView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 盘点记录
 * Created by YoungeTao on 2017/07/20
 * QQ 2276559259.
 * gmail youngetao@gmail.com
 */
public class InventoryRecordActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    private InventoryRecordAdapter<InventoryRecordBean.DataBean> adapter;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_inventory_record;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTitleBar.leftExit(this);

        mTitleBar.setTitle(R.string.check_record);

        adapter = new InventoryRecordAdapter<>();

        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, BaseRecyclerAdapter.CommonHolder holder,
                                    int position) {
                //startActivity(InventoryRecordDetailActivity.class);
                Intent intent = new Intent(InventoryRecordActivity.this,InventoryRecordDetailActivity.class);
                intent.putExtra("dataBean",adapter.getData().get(position));
                startActivity(intent);
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerSpace(getResources(), 0, R.dimen.normal_24dp));
        mRecyclerView.setAdapter(adapter);
        initData();
    }

    private void initData() {
        CountingProto.get().stockRecord(null).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<InventoryRecordBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogPlus.e(e.getMessage());
                    }

                    @Override
                    public void onNext(InventoryRecordBean data) {
                        adapter.setNewData(data.getData());
                    }
                });
    }
}
